package com.woniuxy.yoga.business.web.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.yoga.business.dao.model.BusinessStore;
import com.woniuxy.yoga.business.model.dto.businessstore.BusinessStoreByAccountIdDTO;
import com.woniuxy.yoga.business.model.dto.businessstore.BusinessStoreByIdDTO;
import com.woniuxy.yoga.business.model.dto.businessstore.BusinessStoreDTO;
import com.woniuxy.yoga.business.model.dto.businessstore.SelBusinessStoreDTO;
import com.woniuxy.yoga.business.model.form.businessstore.BusinessStoreSelectPageForm;
import com.woniuxy.yoga.business.model.form.businessstore.BusinessStoreUpdateForm;
import com.woniuxy.yoga.business.model.form.businessstore.UpdateScoreForm;
import com.woniuxy.yoga.business.model.param.businessaccount.UpdateHeaderParam;
import com.woniuxy.yoga.business.model.param.businessstore.BusinessStoreSelectPageParam;
import com.woniuxy.yoga.business.model.param.businessstore.BusinessStoreUpdateParam;
import com.woniuxy.yoga.business.permission.Permission;
import com.woniuxy.yoga.business.service.BusinessStoreService;
import com.woniuxy.yoga.commons.core.model.PageInfo;
import com.woniuxy.yoga.commons.core.model.Result;
import com.woniuxy.yoga.commons.core.util.JWTUtil;
import com.woniuxy.yoga.commons.redis.util.RedisUtil;
import com.woniuxy.yoga.commons.web.log.YogaLog;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 商家门店详情 前端控制器
 * </p>
 *
 * @author 韦用文,刘存益,郎腾霄
 * @since 2023年11月28日
 */
@RestController
@RequestMapping("/businessStore")
@Api(tags = "商家门店相关接口")
@Validated
public class BusinessStoreController {

    @Autowired
    private BusinessStoreService businessStoreService;
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 通过门店ID查询门店信息
     * @param storeId
     * @return
     */
    @GetMapping("/selByStoreId/{storeId}")
    @ApiOperation("通过门店ID查询门店信息")
    @YogaLog("通过门店ID查询门店信息")
    public Result<BusinessStoreDTO> selByStoreId(@PathVariable("storeId") @NotNull Integer storeId){
        BusinessStoreDTO businessStoreDTO = businessStoreService.selByStoreId(storeId);
        return Result.getResult(BusinessStoreDTO.class).setMessage("查询门店信息成功").setData(businessStoreDTO);
    }

    /**
     * 分页查询门店信息
     * @return
     * @throws Exception
     */
    @PostMapping("/selectPageBusinessStore.api")
    @ApiOperation("分页查询门店信息操作")
    @YogaLog("分页查询门店信息")
    @Permission(resources = {"/business/businessStore/selectPageBusinessStore.api"})
    public Result<PageInfo> selectPageBusinessStore(@RequestBody BusinessStoreSelectPageForm form)throws Exception{
        PageInfo pageInfo = businessStoreService.selectBusinessStore(BeanUtil.toBean(form, BusinessStoreSelectPageParam.class));
        return Result.getResult(PageInfo.class).setMessage("查询成功").setData(pageInfo);
    }

    /**
     * 查询门店信息集合
     * @return
     */
    @GetMapping("/selectBusinessStore.api")
    @ApiOperation("查询门店信息集合")
    @YogaLog("查询门店信息集合")
    @Permission(resources = {"/business/businessStore/selectBusinessStore.api"})
    public Result<List<SelBusinessStoreDTO>> selectBusinessStore(){
        List<SelBusinessStoreDTO> selBusinessStoreDTOS = businessStoreService.searchBusinessStore();
        return Result.getListResult(SelBusinessStoreDTO.class).setData(selBusinessStoreDTOS).setMessage("查询门店信息集合成功");
    }

    /**
     * 门店资料变更
     * @param form
     * @return
     * @throws Exception
     */
    @PostMapping("/updateBusinessStore.bu")
    @ApiOperation("门店资料变更操作")
    @YogaLog("门店资料变更")
    public Result<Boolean> updateBusinessStore(@Valid@RequestBody BusinessStoreUpdateForm form)throws Exception{
        Boolean bool = businessStoreService.updateBusinessStore(BeanUtil.toBean(form, BusinessStoreUpdateParam.class));
        if (bool){
            return Result.getResult(Boolean.class).setMessage("门店资料变更成功").setData(bool);
        }
        return Result.getResult(Boolean.class).setMessage("门店没有资料变更").setData(bool);
    }

    /**
     * 注销门店
     * @param storeId
     * @return
     * @throws Exception
     */
    @GetMapping("/deleteBusinessStore/{storeId}.bu")
    @ApiOperation("注销门店操作")
    @YogaLog("注销门店")
    public Result<String> deleteBusinessStore(@PathVariable("storeId") Integer storeId)throws Exception{
        businessStoreService.deleteBusinessStore(storeId);
        return Result.getResult(String.class).setMessage("注销门店成功");
    }

    /**
     * 禁用门店
     * @param storeId
     * @return
     * @throws Exception
     */
    @GetMapping("/disableBusinessStore/{storeId}.bu")
    @ApiOperation("禁用门店")
    @YogaLog("禁用门店")
    public Result<String> disableBusinessStore(@PathVariable("storeId") Integer storeId)throws Exception{
        businessStoreService.disableBusinessStore(storeId);
        return Result.getResult(String.class).setMessage("禁用成功");
    }

    /**
     * 启用门店
     * @param storeId
     * @return
     * @throws Exception
     */
    @GetMapping("/normalBusinessStore/{storeId}.bu")
    @ApiOperation("启用门店")
    @YogaLog("启用门店")
    public Result<String> normalBusinessStore(@PathVariable("storeId") Integer storeId)throws Exception{
        businessStoreService.normalBusinessStore(storeId);
        return Result.getResult(String.class).setMessage("启用成功");
    }

    /**
     * 根据商家id查询门店信息,商家状态正常、门店状态正常且已缴费
     * @param accountId
     * @return
     * @throws Exception
     */
    @GetMapping("/selBusinessStoreByAccountId/{accountId}")
    @ApiOperation("根据商家id查询门店信息")
    @YogaLog("商家ID查询正常门店信息")
    public Result<BusinessStoreByAccountIdDTO> selBusinessStoreByAccountId(@PathVariable("accountId") Integer accountId)throws Exception{
        BusinessStoreByAccountIdDTO dto = businessStoreService.selBusinessStoreByAccountId(accountId);
        return Result.getResult(BusinessStoreByAccountIdDTO.class).setMessage("查询成功").setData(dto);
    }

    /**
     * 商家查询门店信息
     * @param authen_token
     * @return
     * @throws Exception
     */
    @PostMapping("/storeSelBusinessStore.bu")
    @ApiOperation("商家查询门店信息")
    @YogaLog("商家查询门店信息")
    public Result<BusinessStoreByIdDTO> storeSelBusinessStore(@RequestHeader String authen_token)throws Exception{
        String refreshToken = redisUtil.get(authen_token);
        Integer businessId = JWTUtil.getBusinessId(refreshToken);
        BusinessStoreByIdDTO dto = businessStoreService.storeSelBusinessStore(businessId);
        return Result.getResult(BusinessStoreByIdDTO.class).setMessage("查询成功").setData(dto);
    }

    /**
     * 通过门店ID修改门店评分
     * @param form
     * @return
     */
    @PostMapping("/updateScoreById")
    @ApiOperation("通过门店ID修改门店评分")
    @YogaLog("通过门店ID修改门店评分")
    public Result<String> updateScoreById(@RequestBody UpdateScoreForm form){
        BusinessStore businessStore = businessStoreService.getById(form.getStoreId());
        businessStore.setStoreScore(form.getStoreScore());
        businessStoreService.updateById(businessStore);
        return Result.getResult(String.class).setMessage("修改评分成功");
    }

    /**
     * 商家修改门店头像
     * @param image
     * @param authen_token
     * @return
     */
    @PostMapping("/updateStoreHeader.bu")
    @ApiOperation("商家修改门店头像")
    @YogaLog("商家修改门店头像")
    public Result<String> updateStoreHeader(MultipartFile image, @RequestHeader String authen_token) throws Exception {
        String refreshToken = redisUtil.get(authen_token);
        Integer businessId = JWTUtil.getBusinessId(refreshToken);
        UpdateHeaderParam param = new UpdateHeaderParam();
        param.setAccountHeader(image);
        param.setAccountId(businessId);
        businessStoreService.updateStoreHeader(param);
        return Result.getResult(String.class).setMessage("修改门店头像成功");
    }


}

